<?php 
session_start();
require_once ("class/m_extra.php");

	$m = new M_extra();
	$user=$_SESSION['username'];
	
	if (isset($_POST['m'])  &&  $_POST['m']!=""){
		if ($_POST['m']== 'detail'){
			if(isset($_POST['id']) && $_POST['id']!=""){ 
				$id=$_POST['id'];
				//$id=1;
				$result=$m->getData(0,$id,1) ;
				echo json_encode( $result->FetchRow() );
			}else{
				echo "Errro";
			}
		}else if ($_POST['m']== 'add'){
			$ar= $_POST['var'];
			$ar["CreatedBy"]=$user;
			$ar["CreatedDate"]=date( "Y/m/d H:i:s" );
			
			if ($m->checkAmount($ar["Amount"])=="0"){
				echo "error";
			}else{
				//convert date from y/m/d to d/m/y
				list($day, $month, $year) = split('/', $ar["StartDate"]);
				$ar["StartDate"]=$year."/".$month."/".$day;
				
				list($day, $month, $year) = split('/', $ar["EndDate"]);
				$ar["EndDate"]=$year."/".$month."/".$day;
				
				$id=$m->InsertData($ar);
				$result=$m->getData(0,$id,1);
				echo "success";
			}
			//echo "<tr>".genTable( $result->FetchRow() )."</tr>";
			
		}else if ($_POST['m']=='edit'){
			if (isset($_POST['var'])){
				$ar=$_POST['var'];
				$flag=isset($ar);
				
				//convert date from y/m/d to d/m/y
				list($day, $month, $year) = split('/', $ar["StartDate"]);
				$ar["StartDate"]=$year."/".$month."/".$day;
				
				list($day, $month, $year) = split('/', $ar["EndDate"]);
				$ar["EndDate"]=$year."/".$month."/".$day;
				
				$ar["UpdatedBy"]=$user;
				$ar["UpdatedDate"]=date( "Y/m/d H:i:s" );
				
				$sqlScript="select * from tb_extra_discount where IsDeleted=0 and ID='". $_POST['id'] ."'";
				$m->updateData($sqlScript, $ar);
				
			echo 'Completed';
			}
					
		}else if ($_POST['m']=='del'){
			if(isset($_POST['var'])){
				$o=$_POST['var'];
				$m->delData($o["id"]);
			}
		}else if ($_POST['m']== 'search'){
			$type=$_POST['type'];
			$page=$_POST['page'];
			
			$page=isset($page)?$page:1;	
			
			$result=$m->getData($type,null,$page);
			$rows=$m->getRowCount();
			
			$i=1 + (($page-1) * $m->getListPage());
			
			$html="";
			if ($rows > 0){
				while($row = $result->FetchRow()){
					If($i%2 <> 0){
						$html .= "<tr class=row1>";
					} else{
						$html .= "<tr>";
					}
					$html .= genTable($row,$i);
					$html .= "</tr>";
					$i++;
				}
			}else{
				$html="<tr>"
					."<td colspan=8 align='center' ><img src='./images/info.gif'/><b/></td>"
					."</tr>";
			}
			
			//Return result , rows , page
			$ar_result=array('data'=>$html,
							'rows'=>$rows,
							'totalpage'=>$m->getTotalPage());
			echo json_encode( $ar_result );
 
		}
	}else{ 
		if (isset($_GET['c']) && $_GET['c'] !="" ){
				echo json_encode( $m->getDataCBO($_GET['c']));
		}
	}

	function genTable($row,$i){
		global $discountScreen;
			$s.="<td align='center'>".$i."</td>" .
					"<td style='display:none'>".$row["ID"]."</td>" .
					"<td align='center'>".$row["Amount"]."</td>" .
					"<td align='center'>".$row["DisType"]."</td>" .
					"<td align='center'>".$row["DiscountName"]."</td>" .
					"<td align='Center'>".$row["StartDate_"]." - ".$row["EndDate_"]."</td>" .					
					"<td align='center'>".$row["Description"]."</td>" ;
					
			if ($_SESSION["permission"][$discountScreen]['Edit']=='1') 	
				$s.="<td align='center'><img src='./images/edit.png' class='btnEdit' style='cursor:pointer' title='Edit' border='0'/></td>";
			else
				$s.="<td>&nbsp;</td>";
					
			if ($_SESSION["permission"][$discountScreen]['Deleted']=='1') 	
				$s.="<td align='center'><img src='./images/remove.png' class='btnDel' style='cursor:pointer' title='Delete' border='0'/></td>" ;
			else 
				$s.="<td>&nbsp;</td>";
				
		return $s;
	}

?>